home *** CD-ROM | disk | FTP | other *** search
- .OP
-
-
-
-
-
-
-
-
-
- SOME_USEFUL_C_TIME_FUNCTIONS
-
-
- By Bill Bolton
-
- Software Tools,
-
- P.O. Box 80,
-
- Newport Beach,
-
- NSW, 2106
-
- AUSTRALIA
-
-
-
- Phone (02) 997-1018
-
-
- .HESOME USEFUL C TIME FUNCTIONS, By Bill Bolton PAGE #
-
- .PA
- SOME_USEFUL_C_TIME_FUNCTIONS
-
- ABSTRACT
-
- Rather than show why you SHOULD use a programming language
-
- (in this case C) this article endeavours to show why I HAVE used
-
- it. An example program demonstrates some aspects of the language
-
- and is directly useful to owners of the Mountain Hardware 100,000
-
- day clock board as well as being generally useful to others with
-
- real time clock facilities in their systems. A basic knowledge of
-
- C will be required to understand the example program, it is
-
- structured ,commented and brief extra descriptions of the
-
- functions are given.
-
- WHY_C?
-
- While I have admired the C language from afar for a long
-
- time it is only relatively recently that I have discovered its
-
- true delights. Because PASCAL was supposed to be THE language for
-
- structured programming and there was a need for more structure in
-
- my programming efforts, I had given PASCAL a good try. The
-
- system utilities which I was interested in writing just would not
-
- come together properly under PASCAL, I had the distinct feeling
-
- that the language was fighting me rather than helping my
-
- programming efforts. My problems with PASCAL were not limited to
-
- one implementation, PASCAL/M, PASCAL MT+, PASCAL/Z and UCSD
-
- PASCAL all presented similar generic problems. Before the PASCAL
-
- phreaks rise up again, let me stress that there are a lot of
-
- things about PASCAL which seem very worthwhile, but I couldn't
-
- get I to do what the things that I need done, in a way I could
-
- grasp.
-
- So for a time I retreated to assembler and FORTRAN for the
-
- serious stuff and BASIC for the quickies. I found that it was
-
- possible to write reasonably structured programs in BASIC-80 (1)
-
- and FORTRAN 80 if a moderate amount of care was taken but
-
- assembler was still the mainstay. A brief flirtation with FORTH
-
- followed. FORTH showed great promise but the temptation to
-
- produce code which even I couldn't understand a week later was
-
- too great. Still, the idea of an extensible language is
-
- fascinating !
-
- In the meantime I had been reading "The C Programming
-
- Language"(2) and it was clear that C was a language that was
-
- structured but not to verbose or inflexible (my major problems
-
- with PASCAL). It also appeared to have an extensible aspect in
-
- that the overhead in writing, maintaining and using a library of
-
- functions was quite small.
-
- At about this time a major software project of the AWA Data
-
- Group was started in C. I was able to look over the programmer's
-
- shoulder as the project progressed and I liked what I saw. What's
-
- more when the project finished the programmers involved still
-
- seemed sane. I had a chance to see a working C compiler
-
- (Whitesmith's for the PDP-11) in action and to study the
-
- documentation. While the Whitesmith's compiler was out of my
-
- financial range for starters, Leor Zolman had his BDS C compiler
-
- available at a very reasonable price (on reflection I'd say its
-
- an absolute steal) so that was my choice.
-
- As soon as the compiler arrived I was thrown into the
-
- situation that forced me to jump in at the deep end and use it
-
- for a commercial project. I had quoted a client 4 hours to write
-
- a file conversion program in assembler, it soon became obvious
-
- that I had made a major error with my time prediction so I
-
- decided to try and salvage the situation by writing the program
-
- in C. This first programming effort in C went so smoothly that
-
- the project was finished within the budgeted time and the client
-
- got a product with error handling an order of magnitude better
-
- than he would have received from an assembler program. Since then
-
- I have abandoned BASIC almost entirely, only use FORTRAN for
-
- maintenance and have greatly reduced the amount of assembler
-
- programming. The description of C as "a PASCAL that's not afraid
-
- to get its hands dirty"(3) is very, very apt.
-
- If there is one single thing that delayed my entry in the
-
- world of C it is probably the sparsity of references to C in the
-
- microcomputer journals. There have been only a handful of
-
- programs published and often they are examples that don't really
-
- do anything useful (Small C in an obvious exception). Following
-
- is one small effort to correct this situation.
-
- THE_PROGRAM
-
- DATEDEMO is a collection of C functions which will format a
-
- string with the current date and current time fetched from a
-
- Mountain Hardware 100,000 day clock S-100 board. Several date and
-
- time formats are available by specification as one of the
-
- arguments to the primary functions.
-
- The date formats are those used by the nearly all "Western"
-
- countries outside North America (this is an appropriate point to
-
- complain loudly about software packages that lock the user into
-
- MM/DD/YY date formats !!!) but are coded in a very general manner
-
- so that nearly any imaginable date format could be easily
-
- generated and extra formats are easily added. Time formats are
-
- somewhat arbritary and only support 24 hour types (perhaps if
-
- someone is keen they will do an AM/PM 12 hour type and contribute
-
- it to DDJ).
-
- In practice I use the functions to put date and time on in
-
- the page headings of hardcopy output and in some business
-
- packages. The functions live in my standard C function library
-
- and are linked in at compile time. Normally the "#define" state
-
- ments are loaded from a header file CLOCK.H but the are listed in
-
- full here. While the program is written for compilation with the
-
- BDS C compiler (Version 1.4X), it should also compile with the
-
- other C compilers. The only non-standard library function is
-
- 'initw' which is the BDS kludge to allow array initialisation, as
-
- BDS C does not support initialisers. As far as I know all other
-
- library functions are supported by other C compilers (maybe not
-
- Small C).
-
- THE_FUNCTIONS
-
- 'main' - is simply an executable demonstration program to
-
- tie the actual working functions together, all the
-
- presently supported formats are displayed and a
-
- typical use of the formatted strings is shown on
-
- the last line displayed.
-
- 'date' - Attempts to get basic data to format a date string
-
- form the clock, if this fails an error message is
-
- displayed. Else the month and week name are
-
- determined and the string formatted.
-
- 'get_date' - tests if the clock board is in the system. Then
-
- collects digits from the clock to get the number of
-
- days since 31/Dec/1977 (N.B. this day was a
-
- Saturday, so a simple MOD 7 on the total number of
-
- days since then makes Sunday day 1). This value is
-
- then turned into the year, month and day of the
-
- month.
-
- 'month_day'- Uses an initialised array to determine month and
-
- day of the month from year and day of the year.
-
- 'ndays' - Returns number of days in the specified year.
-
- 'leap' - Thorough test for leap years.
-
- 'name_month'- Fills a string with a month name given the number
-
- of the month in range 1 to 12.
-
- 'name_week'- Fills a string with day of the week name given the
-
- number of the day in range 0 to 6.
-
- 'time' - As for 'date' except that time needs much less
-
- processing.
-
- 'read_clock'- Tests if clock is in system (this test must be
-
- duplicated from 'get_date' as the date and time
-
- functions may be used separately), then fetches
-
- digits from the clock and puts them straight into
-
- an integer array.
-
-
- THE HARDWARE
-
- The MH board occupies a block of 16 I/O ports. The values
-
- returned from each port are shown at the head of the listing.
-
- Each value is in the form of a BCD number and for the purposes of
-
- this program the high nybble of the port can be ignored. A simple
-
- test for presense of the board in the bus can be made by looking
-
- at the base port, an FF Hex will indicate that the board is
-
- missing. An on-board battery keeps the clock running all the
-
- time, I only need to reset time for Daylight saving and usually
-
- do it using DDT's inline assembler. The board has been super
-
- reliable and functions well in a system set up to IEEE 696
-
- standards.
-
- The rest of the system is a North Star Horizon mainframe
-
- with a Morrow Designs "Wunderbuss" 12 slot motherboard (Yes it IS
-
- possible to get another motherboard into an Horizon, and
-
- attacking your computer with a hammer can be a very theraputic
-
- experience, but that is a story for another time) Godbout Z-80
-
- CPU, Morrow SuperRam memory, Morrow Disk Jockey 2D controller and
-
- Godbout Interfacer I Serial I/O.
-
- PROGRAM_AVAILABLITY
-
- This program will be available from the BDS C User's Group,
-
- 409 E. Kansas, Yates Centre, KS 66783. Membership of the group is
-
- US$10:00 a year (US$20:00 a year foreign) and numerous disks of C
-
- software are available from the group for nominal copying
-
- charges. Amongst the software available is Ron Cain's Small C
-
- Compiler and Run Time Library previously published in DDJ, and
-
- also W.C. Colley's 6800 & 1802 cross assemblers previously
-
- described in DDJ.
-
-
-
- *****************************************************************
-
- References:
-
- (1) See CPMLABLE.BAS on CP/M User's Group Disk 46, one of my
-
- efforts at structure in BASIC 80.
-
- (2) "The C programming Language" By Brian W. Kernighan and
-
- Dennis M. Ritchie, Published by Prentice-Hall 1978, ISBN 0-13-
-
- 110163-3. This is the "Bible" of C and essential to any study of
-
- the language.
-
- (3) From "C Notes" by C.T Zahn, Published By Yourdon Press 1979,
-
- ISBN 0-917072-13-8. The only other readily available work on C,
-
- interesting as it was written by someone outside Bell Labs and
-
- has a slightly different perspective on the language.
-
- *****************************************************************
-
- THE_AUTHORP
-
- I am employed as Senior Technical Officer at Multicultural
-
- Television Channel 0/28, involved in the maintenance of both mini
-
- and microcomputer systems as well as video systems. BASIC and
-
- FORTRAN were learned as part of a college course in 1975 and that
-
- was were I became interested in computers. Present activities
-
- include consulting work for CP/M system users, cordinating 80AT
-
- (the Australian 8080/Z80 User's Group) and writing a regular CP/M
-
- column in a local microcomputer journal "Your Computer".